home *** CD-ROM | disk | FTP | other *** search
- Path: news.luc.edu!user
- From: VArase@varase.it.luc.edu (Verne Arase)
- Newsgroups: comp.lang.c
- Subject: Re: GOTO controversy
- Date: Fri, 08 Mar 1996 12:42:06 -0600
- Organization: LUMC
- Message-ID: <AD65DB9E9668BB9B8@mcdiala11.it.luc.edu>
- References: <rcshlds.1.000A6705@mailserv.mta.ca> <Dn8pJ8.nqs@emi.net> <4grt4e$8fg@goanna.cs.rmit.EDU.AU> <4hl8mt$4po@newshost.cyberramp.net> <DnwCxp.84C@clw.cs.man.ac.uk>
- NNTP-Posting-Host: 147.126.240.111
-
- In article <DnwCxp.84C@clw.cs.man.ac.uk>,
- chl@clw.cs.man.ac.uk (Charles Lindsey) wrote:
-
- >In <4hl8mt$4po@newshost.cyberramp.net> sinan@cyberramp.net (John Noland)
- writes:
- >
- >>>Robert C Shields (rcshlds@mailserv.mta.ca) wrote:
- >>>Example for a good use of goto:
- >>>--- ????
-
- >>>------------------------------------------------------------------------------
- >>> HEV hev1, hev2, hev3; /* Event semaphores */
- >>> HMTX hmtx; /* Mutex semaphore */
- >>> void *ptr;
- >
- >>> if (!DosCreateEventSem(0, &hev1, 0, FALSE))
- >>> goto hev1_failed;
- >>>
- >>> if (!DosCreateEventSem(0, &hev2, 0, FALSE))
- >>> goto hev2_failed;
- >
- >>> if (!DosCreateEventSem(0, &hev3, 0, FALSE))
- >>> goto hev3_failed;
- >>>
- >>> if (!DosCreateMutexSem(0, &hmtx, 0, FALSE))
- >>> goto hmtx_failed;
- >>>
- >>> if ((ptr = malloc(SOME_SIZE)) == NULL)
- >>> goto malloc_failed;
- >>>
- >>> /* Do some stuff here */
- >>> return TRUE; /* We did okay */
- >>>
- >>>malloc_failed:
- >>> DosCloseMutexSem(hmtx);
- >>>hmtx_failed:
- >>> DosCloseEventSem(hev3);
- >>>hev3_failed:
- >>> DosCloseEventSem(hev2);
- >>>hev2_failed:
- >>> DosCloseEventSem(hev1);
- >>>hev1_failed:
- >>> return FALSE;
- >
- >
- >>The above is NOT an example of a good use for the goto statement.
- >
- >Well that is what I thougut when I first saw the example, and I was about
- to
- >dash off an article like you have done. But then I looked again and saw
- >*exactly* what it was doing.
- >
- >So if you believe there is a better way, please can we see it?
-
- How about:
-
- HEV hev1,hev2,hev3; /* Event semaphores */
- HMTX hmtx; /* Mutex semaphore */
- void *ptr;
-
- if (DosCreateEventSem(0,&hev1,0,FALSE)) {
- if (DosCreateEventSem(0,&hev2,0,FALSE)) {
- if (DosCreateEventSem(0,&hev3,0,FALSE)) {
- if (DosCreateMutexSem(0,&hmtx,0,FALSE)) {
- if (ptr=malloc(SOME_SIZE))
- return TRUE;
- DosCloseMutexSem(htmx);
- }
- DosCloseEventSem(hev3);
- }
- DosCloseEventSem(hev2);
- }
- DosCloseEventSem(hev1);
- }
- return FALSE;
-
- BTW, I've removed comp.lang.pl1 and comp.lang.misc from this thread; I call
- on others to do likewise.
-
- ---
- The above are my own opinions, and not those of my employer.
-